package com.sumer.psychology.photo.infrastructure.persistence.mongo;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.sumer.psychology.common.notification.NotificationReader;
import com.sumer.psychology.common.persistence.mongodb.AbstractSimpleMongoRepository;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;

import javax.inject.Inject;

/**
 * mongo相册资源库
 * <p/>
 * User: zhenguang.zhu
 * Date: 2014/10/29
 * Time: 15:01
 */
@Aspect
public class MongoAlbumRepository extends AbstractSimpleMongoRepository {
    private static final String DB_NAME = "sumer_album";
    private static final String ALBUM_KEY = "albumId.identity";

    @Inject
    private MongoPhotoRepository mongoPhotoRepository;

    /**
     * 新建一个相册
     *
     * @param data json
     */
    @Override
    public void add(String data) {
        DBObject album = initializeCreateData(data, ALBUM_KEY);
        album.put("photos", 0);

        db().getCollection(dbName()).insert(album);
    }

    /**
     * 增加照片
     *
     * @param data data
     */
    @After(value = "execution(* com.sumer.psychology.photo.infrastructure.persistence.mongo.MongoPhotoRepository.add(String)) && args(data)",
            argNames = "data")
    public void increasePhotos(String data) {
        NotificationReader reader = new NotificationReader(data);
        String albumId = reader.eventStringValue("albumId.identity");

        db().getCollection(dbName()).update(
                new BasicDBObject("_id", albumId),
                new BasicDBObject("$inc", new BasicDBObject("photos", 1)));

        setDefaultCover(reader, albumId);
    }

    /**
     * 删除照片
     *
     * @param photoId photo id
     */
    public void removePhoto(String photoId) {
        DBObject photo = mongoPhotoRepository.findOne(photoId);

        db().getCollection(dbName()).update(
                new BasicDBObject("_id", photo.get("albumId")),
                new BasicDBObject("$inc", new BasicDBObject("photos", -1)));
    }

    /*
     * 默认封面是第一个照片
     */
    private void setDefaultCover(NotificationReader reader, String albumId) {
        DBObject album = findOne(albumId);
        Integer photos = (Integer) album.get("photos");
        if (photos == 1) {
            String photoId = reader.eventStringValue("photoId.identity");
            String fileId = reader.eventStringValue("fileId");
            setCover(photoId, fileId, albumId);
        }
    }

    /**
     * 设置相册封面
     *
     * @param photoId photo
     * @param fileId  file
     * @param albumId album
     */
    public void setCover(String photoId, String fileId, String albumId) {
        DBObject coverPhoto = new BasicDBObject();
        coverPhoto.put("_id", photoId);
        coverPhoto.put("fileId", fileId);

        db().getCollection(dbName()).update(
                new BasicDBObject("_id", albumId),
                new BasicDBObject("$set",
                        new BasicDBObject("cover_photo", coverPhoto)));
    }

    @Override
    protected String dbName() {
        return DB_NAME;
    }

    @Override
    protected String key() {
        return ALBUM_KEY;
    }
}
